from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client
from meiduo_mall import settings
from django.utils.deconstruct import deconstructible


@deconstructible
class MyStorage(Storage):

    def _open(self,name,mode='rb'):
        pass
    def _save(self,name,content,max_length=None):
        # 1.創建客戶端
        client = Fdfs_client('utils/fdfs/client.conf')
        # 2.獲取圖片資源
        data = content.read()
        # 3.上傳
        result = client.upload_by_buffer(data)
        """
        {'Status': 'Upload successed.',
        'Uploaded size': '215.00KB',
        'Local file name': '/home/python/Desktop/01.png',
        'Group name': 'group1',
        'Storage IP': '192.168.31.142',
        'Remote file_id': 'group1/M00/00/00/wKgfjlylyDGAFQ7xAANdiM2Nt8w841.png'}

        """
        # 根據上傳狀態,獲取 Remote_file_id
        if result.get('Status') == 'Upload successed.':
            return result.get('Remote file_id')

    def exists(self, name):
        return False

    def url(self, name):

        return 'http://192.168.31.142:8888/'+name
